class Solution {
public:
    int lengthOfLastWord(const char *s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (s == NULL || *s == 0) {
            return 0;
        }
        int n = strlen(s);
        int ans = 0;

        for (int i = 0; i < n; ) {
            int start = i;
            while (start < n && s[start] == ' ') {
                ++start;
            }
            int end = start;
            while (end < n && s[end] != ' ') {
                ++end;
            }
            if (end - start > 0) {
                ans = end - start;
            }
            i = end;
        }
        return ans;

    }
};
